package com.nengchuan.kbms.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nengchuan.model.dto.PageDTO;

import java.util.List;

/**
 * xml中的查询如果需要分页 ，那么将查询对象（原继承自 PageDTO）继承此类即可，
 * @param <T>
 */
public class XmlPageDTO<T> extends PageDTO implements IPage<T> {

    private Page<T> page = new Page<>();

    private QueryWrapper<T> wrapper = null;

    public XmlPageDTO() {}

    /**
     * for mapper.xml 自定义条件
     */
    @JsonIgnore
    public QueryWrapper<T> getEw() {
        if (wrapper == null) {
            synchronized (this) {
                if (wrapper == null) {
                    wrapper = new AutoWrapper<>(this);
                }
            }
        }
        return wrapper;
    }

    @JsonIgnore
    public void asc(String... column) {
        page.addOrder(OrderItem.ascs(column));
    }

    @JsonIgnore
    public void desc(String... column) {
        page.addOrder(OrderItem.descs(column));
    }

    @JsonIgnore
    @Override
    public List<OrderItem> orders() {
        return page.getOrders();
    }

    @JsonIgnore
    @Override
    public List<T> getRecords() {
        return page.getRecords();
    }

    @JsonIgnore
    @Override
    public IPage<T> setRecords(List<T> records) {
        return page.setRecords(records);
    }

    @JsonIgnore
    @Override
    public long getTotal() {
        return page.getTotal();
    }

    @JsonIgnore
    @Override
    public IPage<T> setTotal(long total) {
        return page.setTotal(total);
    }

    @JsonIgnore
    @Override
    public long getSize() {
        return page.getSize();
    }

    @JsonIgnore
    @Override
    public IPage<T> setSize(long size) {
        return page.setSize(size);
    }

    @JsonIgnore
    @Override
    public long getCurrent() {
        return page.getCurrent();
    }

    @JsonIgnore
    @Override
    public IPage<T> setCurrent(long current) {
        return page.setCurrent(current);
    }

    @JsonIgnore
    @Override
    public boolean isSearchCount() {
        return page.isSearchCount();
    }

    @JsonIgnore
    @Override
    public long getPages() {
        return page.getPages();
    }

    @Override
    public void setPageNum(Integer pageNum) {
        super.setPageNum(pageNum);
        setCurrent(pageNum);
    }

    @Override
    public void setPageSize(Integer pageSize) {
        super.setPageSize(pageSize);
        setSize(pageSize);
    }
}
